package com.guozy.example.rpc;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * spring容器启动完成之后，会发布一个ContextRefreshedEvent
 */
@Component
public class SocketServerInitial implements ApplicationListener {

    private final ExecutorService executorService = Executors.newCachedThreadPool();

    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        // 启动服务
        ServerSocket serverSocket =null;
        try {
            serverSocket = new ServerSocket(8088);
            while (true) {
                Socket socket = serverSocket.accept(); // 监听客户端请求
                executorService.execute(new ProcessHandler(socket));

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
